package com.zhudoo.zhilian.api;

import com.alibaba.fastjson.JSON;
import com.qiniu.common.Zone;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import com.zhudoo.zhilian.config.Config;
import com.zhudoo.zhilian.service.CityService;
import com.zhudoo.zhilian.service.UserService;
import com.zhudoo.zhilian.util.web.Response;
import com.zhudoo.zhilian.util.web.ResponseUtil;
import com.zhudoo.zhilian.util.web.ServiceContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author zhudao
 */
@RestController
@RequestMapping("/common")
public class CommonController {

    @Autowired
    CityService cityService;
    @Autowired
    UserService userService;

    /**
     * 文件上传
     *
     * @param file
     * @return
     */
    @RequestMapping(value = "/uploadImage", method = RequestMethod.POST)
    public Response uploadImage(MultipartFile file) throws IOException {
        if (!file.isEmpty()) {
            Configuration cfg = new Configuration(Zone.zone0());
            UploadManager uploadManager = new UploadManager(cfg);
            Auth auth = Auth.create(Config.QINIU_ACCESSKEY, Config.QINIU_SECRETKEY);
            String token = auth.uploadToken(Config.QINIU_BUCKETNAME);
            com.qiniu.http.Response response = uploadManager.put(file.getBytes(), null, token);
            DefaultPutRet putRet = JSON.parseObject(response.bodyString(), DefaultPutRet.class);
            return ResponseUtil.responseSuccess(ResponseUtil.SUCCESS, Config.QINIU_URL + putRet.hash);
        }
        return ResponseUtil.responseFailure("上传图片失败");
    }

    /**
     * 收集formId
     *
     * @param formId
     * @param info
     * @return
     */
    @RequestMapping(value = "/submitFormId", method = RequestMethod.POST)
    public Response submitFormId(@RequestHeader String token, String formId, String info) {
        ServiceContext serviceContext = ServiceContext.getInstance();
        serviceContext.addParams("formId", formId);
        serviceContext.addParams("info", info);
        serviceContext.addParams("token", token);
        userService.submitFormId(serviceContext);
        return ResponseUtil.responseSuccess("成功");
    }

    @RequestMapping(value = "/getCityList", method = RequestMethod.GET)
    public Response getCityList() {
        Object resultObj = cityService.getCityList();
        return ResponseUtil.responseSuccess("成功", resultObj);
    }

    @RequestMapping(value = "/getLineList", method = RequestMethod.GET)
    public Response getLineList(Integer cityId) {
        ServiceContext serviceContext = ServiceContext.getInstance();
        serviceContext.addParams("cityId", cityId);
        Object resultObj = cityService.getLineList(serviceContext);
        return ResponseUtil.responseSuccess("成功", resultObj);
    }

}
